package com.mine.service;

import com.mine.dao.CartMapper;
import com.mine.dao.MealMapper;
import com.mine.dao.UserMapper;
import com.mine.pojo.Cart;
import com.mine.pojo.Meal;
import com.mine.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class MealService {

    @Autowired
    MealMapper mealMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    UserMapper userMapper;

    public List<Meal> queryAllMeal() {
        return mealMapper.queryAllMeal();
    }

    public Meal getMealById(Integer id) {
        return mealMapper.getMealById(id);
    }

    public List<Meal> queryBargainpriceMeal() {
        return mealMapper.queryBargainpriceMeal();
    }

    public List<Cart> querynewTitle() {
        return cartMapper.querynewTitle();
    }

    @Transactional
    public List<Meal> shopList(String class0, String class1, String select_name) {
        List<Meal> mealList = null;
        String condition1 = "";
        if ("0".equals(class0)) {
            condition1 = "";
        } else if ("1".equals(class0)) {
            condition1 = "荤菜";
        } else if ("2".equals(class0)) {
            condition1 = "素菜";
        } else if ("3".equals(class0)) {
            condition1 = "主食";
        } else {
            condition1 = "饮品";
        }
        if ("0".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 0, 10000, select_name);
        } else if ("20".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 0, 20, select_name);
        } else if ("40".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 20, 40, select_name);
        } else if ("60".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 40, 60, select_name);
        } else if ("80".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 60, 80, select_name);
        } else if ("100".equals(class1)) {
            mealList = mealMapper.shopList(condition1, 80, 100, select_name);
        } else {
            mealList = mealMapper.shopList(condition1, 100, 10000, select_name);
        }
        return mealList;
    }

    @Transactional
    public void mealCollect(Integer meal_id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        mealMapper.addMealCollect(meal_id);
        userMapper.addMealCollect(user.getCollect() + "|" + meal_id.toString(), user.getUsername());
        User user1 = userMapper.getUserByName(user.getUsername());
        session.setAttribute("user", user1);
    }

    @Transactional
    public void mealCollectOver(Integer meal_id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        mealMapper.overMealCollect(meal_id);
        String collect = "0";
        String[] split = user.getCollect().split("\\|");
        for(int i=1;i<split.length;i++){
            if(!split[i].equals(meal_id.toString())){
                collect = collect + "|" + split[i];
            }
        }
        userMapper.addMealCollect(collect, user.getUsername());
        User user1 = userMapper.getUserByName(user.getUsername());
        session.setAttribute("user", user1);
    }

    //修改菜品
    @Transactional
    public void updateMealInformation(HttpSession session, MultipartFile file, Meal meal) throws IOException {
        Meal meal1 = mealMapper.getMealById(meal.getMeal_id());
        if (!file.isEmpty()) {
            String imgUrl = saveFile(file, session);
            //删除原来的图片
            String path = ResourceUtils.getURL("classpath:").getPath();
            File filePath = new File(path + "/static");
            File finalFile = new File(filePath, meal1.getMeal_image());
            if (finalFile.exists()) {
                finalFile.delete();//删除原来的文件
            }
            meal.setMeal_image(imgUrl);
        } else {
            meal.setMeal_image(meal1.getMeal_image());
        }
        mealMapper.updateMealInformation(meal);
    }

    String saveFile(MultipartFile file, HttpSession session) throws IOException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        File filePath = new File(path + "/static/upload");
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        String originalFileName = file.getOriginalFilename();
        StringBuffer fileName = new StringBuffer(UUID.randomUUID().toString().replace("-", ""));
        fileName.append("_").append(originalFileName);
        File finalFile = new File(filePath, fileName.toString());
        file.transferTo(finalFile);
        return "/upload/" + fileName.toString();
    }

    //增加商品
    @Transactional
    public void addMealInformation(HttpSession session, MultipartFile file, Meal meal) throws IOException {
        String imgUrl = saveFile(file, session);
        //删除原来的图片
        meal.setMeal_image(imgUrl);
        System.out.println("---------------"+meal);
        mealMapper.addMealInformation(meal);
    }
    //删除菜品
    @Transactional
    public void remove_meal(String meal_id) {
        mealMapper.remove_meal(meal_id);
    }
}
